package com.evon.yardmanagement.export;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.evon.yardmanagement.model.KeyBin;
import com.evon.yardmanagement.model.KeyBox;

/**
 * Exportable and importable data for Key Box.
 * 
 * @author vgaurav
 * 
 */

public class KeyBoxExportableData implements ExportableData,
		ImportableData<KeyBox> {

	// Data to be exported
	private List<KeyBox> keyBoxDataList;

	private File fileToImport;

	private static final int COLUMN_KEYBIN_ID = 9;

	private static final int COLUMN_UPDATED_USER = 8;

	private static final int COLUMN_CREATED_USER = 7;

	private static final int COLUMN_UPDATED_TIME = 6;

	private static final int COLUMN_CREATED_TIME = 5;
	//
	private static final int COLUMN_IS_ACTIVE = 4;
	//
	private static final int COLUMN_OCCUPIED = 3;
	//
	private static final int COLUMN_KEYBOX_NAME = 2;
	//
	private static final int COLUMN_KEYBOX_NUMBER = 1;
	//
	private static final int COLUMN_KEYBOX_ID = 0;

	private static final String KEYBIN_ID = "KEYBIN_ID";

	private static final String CAPACITY = "CAPACITY";

	private static final String HSC_ID = "HSC_ID";

	private static final String HSU_TS = "HSU_TS";

	private static final String HSC_TS = "HSC_TS";
	//
	private static final String ACTIVE = "ACTIVE";
	//
	private static final String OCCUPIED = "OCCUPIED";
	//
	private static final String KEYBOX_NAME = "KEYBOX_NAME";
	//
	private static final String KEYBOX_NUMBER = "KEYBOX_NUMBER";
	//
	private static final String KEYBOX_ID = "KEYBOX_ID";

	private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");;

	public KeyBoxExportableData(List<KeyBox> keyBoxes) {
		this.keyBoxDataList = keyBoxes;
	}

	public void setValueAt(int row, int column, Object value) {
		if (keyBoxDataList == null) {
			keyBoxDataList = new ArrayList<KeyBox>();
		}
		if (column > getColumnCount()) {
			throw new IllegalArgumentException(
					"Column size can not be greater than " + getColumnCount());
		}

		if (keyBoxDataList.size() - 1 < row) {
			keyBoxDataList.add(new KeyBox());
		}
		KeyBox keyBox = keyBoxDataList.get(row);
		if (value == null) {
			return;
		}

		switch (column) {
		case COLUMN_KEYBOX_ID:
			// for new Section

			keyBox.setId((Integer) value);

			break;
		case COLUMN_KEYBIN_ID:
			KeyBin keyBin = new KeyBin();
			keyBin.setId((Integer) value);
			keyBox.setKeyBin(keyBin);
			break;
		case COLUMN_KEYBOX_NAME:
			keyBox.setName((String) value);
			break;

		case COLUMN_KEYBOX_NUMBER:
			if (value instanceof String)
				keyBox.setBoxNumber(Integer.valueOf((String) value));
			else
				keyBox.setBoxNumber((Integer) value);
			break;

		case COLUMN_OCCUPIED:
			if (value instanceof String)
				keyBox.setOccupied(Integer.valueOf((String) value));
			else
				keyBox.setOccupied((Integer) value);
			break;

		case COLUMN_IS_ACTIVE:
			if (value instanceof String)
				keyBox.setIsActive(Integer.valueOf((String) value));
			else
				keyBox.setIsActive((Integer) value);
			break;
		case COLUMN_CREATED_TIME:
			DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Date d;
			try {
				d = (Date) formatter.parse((String) value);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				d = new java.util.Date();
			}
			keyBox.setCreatedDate(d);
			break;
		case COLUMN_UPDATED_TIME:
			DateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Date d1;
			try {
				d1 = (Date) formatter1.parse((String) value);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				d1 = new java.util.Date();
			}
			keyBox.setUpdatedDate(d1);
			break;

		case COLUMN_CREATED_USER:
			keyBox.setCreatedBy((String) value);
			break;

		case COLUMN_UPDATED_USER:
			keyBox.setUpdatedBy((String) value);
			break;

		default:
			throw new IllegalArgumentException("Column size is not correct- "
					+ column);
		}

	}

	public File getFileToImport() {
		// TODO Auto-generated method stub
		return fileToImport;
	}

	public void setFileToImport(File fileToImport) {
		this.fileToImport = fileToImport;

	}

	public List<KeyBox> getData() {
		if (keyBoxDataList == null || keyBoxDataList.isEmpty()) {
			return Collections.EMPTY_LIST;
		}
		return keyBoxDataList;
	}

	public int getColumnCount() {
		return 11;
	}

	public int getRowCount() {
		if (keyBoxDataList == null || keyBoxDataList.isEmpty()) {
			return 0;
		}
		return keyBoxDataList.size();
	}

	public List<String> getColumnHeader() {
		return Arrays.asList(KEYBOX_ID, KEYBOX_NUMBER, KEYBOX_NAME, OCCUPIED,
				ACTIVE, HSC_TS, HSU_TS, HSC_ID, HSC_ID, CAPACITY, KEYBIN_ID);
	}

	public String getValueAt(int row, int column) {
		if (keyBoxDataList == null || keyBoxDataList.isEmpty()) {
			throw new IllegalStateException(
					"The KeyBoxExportableData has no model set, please set the list of model first before calling this method.");
		}
		if (row >= keyBoxDataList.size() || column > getColumnCount()) {
			throw new IllegalArgumentException(
					"The row and column range is out of bound. Chcek the row and column size being passed to this method.");
		}

		KeyBox keyBox = keyBoxDataList.get(row);

		if (keyBox == null) {
			throw new IllegalStateException(
					"Key Box inside keyBoxDataList is null.");
		}

		switch (column) {
		case COLUMN_KEYBOX_ID:
			return String.valueOf(keyBox.getId());
		case COLUMN_KEYBIN_ID:
			return String.valueOf(keyBox.getKeyBin().getId());
		case COLUMN_KEYBOX_NUMBER:
			return String.valueOf(keyBox.getBoxNumber());
		case COLUMN_KEYBOX_NAME:
			return keyBox.getName();
		case COLUMN_IS_ACTIVE:
			return String.valueOf(keyBox.getIsActive());

		case COLUMN_OCCUPIED:
			return String.valueOf(keyBox.getOccupied());
		case COLUMN_CREATED_TIME:
			if (keyBox.getCreatedTime() != null)
				return formatter.format(keyBox.getCreatedTime());
			else
				formatter.format(new Date());
		case COLUMN_UPDATED_TIME:
			if (keyBox.getLastUpdatedTime() != null)
				return formatter.format(keyBox.getLastUpdatedTime());
			else
				formatter.format(new Date());
		case COLUMN_CREATED_USER:
			return keyBox.getCreatedUserName();
		case COLUMN_UPDATED_USER:
			return keyBox.getLastUpdatedUserName();
		default:
			return "";
		}
	}

	public String getDataName() {
		// TODO Auto-generated method stub
		return "KEYBINS";
	}

	public ExportCellType getColumnClass(int columnNumber) {
		// TODO Auto-generated method stub
		return null;
	}

}
